#ifndef BSP2ZONE_H_
#define BSP2ZONE_H_

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <map>

#include "zone.h"
#include "segment.h"
#include "irrlicht.h"   // For vector3df
#include "bsp_reader.h"
#include "graph_directed.h"
#include "algo_dijkstra.h"

#include "Exception.h"

using irr::core::vector3df;
using irr::core::vector2df;
using irr::s32;
using irr::u32;

/*! The bsp2zone class converts all the informations found in the bsp reader (Bsp from bsp_reader.h) into a list of usable zones for pathfinding. It is currently under construction. */
class bsp2zone
{
	public:
	std::list<segment_t> tmp____;

	protected:
	typedef std::pair<vector2df, vector2df> seg2d_t;
	bool intersec(seg2d_t s1, seg2d_t s2);
	float det(vector2df p1, vector2df p2);

	graph::directed<zone, segment_t> zone_graph__; /**< The graph of zones. Used at compile time. */
	graph::directed<segment_t, float> waypoint_graph__; /**< The graph of waypoint. */

	/*! Returns the model number of a string representing the model in the form "*x" where 'x' represents the model number. */
	int getModel(const std::string& m);

	void generateVertecesOfZoneGraph();

	segment_t getIntersection(segment_t s1, segment_t s2);

	protected:
	void computeNeighbors(unsigned int it1, unsigned int it2);
	void sortZonePoints(Bsp& b, unsigned int vert_offset, int& swap1, int& swap2);

	public:
	/*! Constructor. Does nothing. */
	bsp2zone();
	/*! Destructor. Does nothing. */
	virtual ~bsp2zone();

	/*! Accessor. Returns the zone graph. Note it's not a const reference. */
	graph::directed<zone, segment_t>& getZones();

	/*! Accessor. Returns the waypoint graph. Note it's not a const reference. */
	graph::directed<segment_t, float>& getWaypoints();

	/*! The method retrieves all zones from a Bsp, and store the results the zone graph. Of course, the Bsp reader must already have read the bsp file before calling this method. If misc informations are added to the waypoint entities, this method should be updated to be able to use them (which is yet a feature to do). */
	void computeZones(Bsp& b);

	void computeZoneNeighbors();

	/*! The method converts a zone graph to a waypoint graph. Must be called after computeZones(Bsp& b) */
	void computeWayPointGraph();

	void computeDijkstraAndSaveToFile(const std::string& file);
};

#endif

